package com.freesky.provider.search.service.impl;

import com.freesky.common.dao.entity.SearchExpandLexcion;
import com.freesky.common.enums.ResponseEnum;
import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.provider.search.service.IExpandLexiconService;
import com.freesky.provider.search.util.ExpandLexiconUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 14:29 2019-01-08
 * @description:
 */
@Service
@Transactional
public class ExpandLexiconServiceImpl implements IExpandLexiconService {
    @Value("${solr.ik-ext-path}")
    private String ikExtPath;

    @Override
    public DataTablesResult getList() {
        DataTablesResult result = new DataTablesResult();
        result.setData(new SearchExpandLexcion().selectAll());
        result.setSuccess(true);
        return result;
    }

    @Override
    public JSONResponse add(SearchExpandLexcion searchExpandLexcion) {
        searchExpandLexcion.insert();
        ExpandLexiconUtil.addLexicon(searchExpandLexcion.getValue(), ikExtPath);
        return JSONResponseDiretor.buildSuccessJSONResponse(ResponseEnum.ADD_EXPAND_LEXICON_SUCCESS, null);
    }

    @Override
    public JSONResponse deleteById(Integer[] ids) {
        for (Integer id:ids) {
            SearchExpandLexcion lexcion = new SearchExpandLexcion(id).selectById();
            ExpandLexiconUtil.deleteLexicon(lexcion.getValue(), ikExtPath);
            lexcion.deleteById();
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(ResponseEnum.DELETE_EXPAND_LEXICON_SUCCESS, null);
    }
}
